/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.enterprise.web;

import com.assg2.dao.DaoViewer;
import com.movie.Viewer;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Kevin
 */
public class RegisterCheck extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            

            
            ServletContext context = getServletContext();
           // context.getAttribute("username");
            String username = (String)context.getAttribute("username");
            String password = (String)context.getAttribute("password");
            String email = (String)context.getAttribute("email");
           // out.print(username);
            String usercode = request.getParameter("usercode");
            DaoViewer dv = new DaoViewer();
            Viewer v = (Viewer) dv.findBy("username", username);
            request.getRequestDispatcher("/header.html").include(request, response);
           // String name = UrlEncode.Decode(usercode);
            if(usercode == null || usercode.equals("")){
             out.print("<b>The confirm email has send to your mailbox, please check!<b>");
             request.getRequestDispatcher("/footer.html").include(request, response);
            }else
            
            if(!username.equals(UrlEncode.Decode(usercode))){
                out.println("Link is not available! ");
            }else if(v != null) {
            out.println("<BODY>sorry, you have already registered<BR>");

            out.println("<A HREF='dispatcher?operation=login'>go to login page</A>");

            out.println("</BODY></HTML>");
            request.getRequestDispatcher("/footer.html").include(request, response);
            }
            else{
          //  out.print("success");
            Viewer viewer = new Viewer();
         //   DaoViewer dv = new DaoViewer();
            //System.out.print(username);
            viewer.setUsername(username);
            viewer.setPassword(password);
            viewer.setEmail(email);
            
            dv.save(viewer);
            
            request.setAttribute("message", " <script laguage='JavaScript'> alert('‚Äö√Ñ√∂‚àö√ë‚àö‚àÇ‚Äö√†√∂‚àö√´‚Äö√Ñ√∂‚àö‚Ä†‚àö¬™‚Äö√Ñ√∂‚àö‚Ä†‚àö‚àÇ‚Äö√Ñ√∂‚àö‚Ä†‚àö¬Æ‚Äö√Ñ√∂‚àö‚Ä†‚àö‚àÇ‚Äö√†√∂‚àö‚à´‚Äö√Ñ√∂‚àö‚Ä†‚àö‚àÇ‚Äö√†√∂¬¨√ü‚Äö√Ñ√∂‚àö‚Ä†‚àö‚àÇ‚Äö√†√∂‚àö√∫‚Äö√¢√†‚àö¬®‚Äö√†√∂‚àö√´‚Äö√Ñ√∂‚àö‚Ä†‚àö‚àÇ‚Äö√†√∂¬¨√ü‚Äö√Ñ√∂‚àö‚Ä†‚àö‚àÇ‚Äö√Ñ√∂‚àö√ë‚Äö√Ñ‚Ä†‚Äö√Ñ√∂‚àö‚Ä†‚àö‚àÇ‚Äö√Ñ√∂‚àö√´¬¨¬¢‚Äö√Ñ√∂‚àö‚Ä†‚àö‚àÇ‚Äö√†√∂‚àö¬∞‚Äö√Ñ√∂‚àö‚Ä†‚àö‚àÇ¬¨¬®‚àö√º‚Äö√Ñ√∂‚àö‚Ä†‚àö‚àÇ¬¨¬®‚Äö√†¬¥) </script>"); 
            //out.print("<script type='javascript/text'>alert('‚Äö√Ñ√∂‚àö‚Ä†‚àö‚àÇ‚Äö√†√∂¬¨√ü‚Äö√Ñ√∂‚àö√ë‚àö‚àÇ‚Äö√†√∂¬¨¬¢‚Äö√Ñ√∂‚àö√ë¬¨¬¢¬¨¬®¬¨¬Æ‚Äö√†√∂‚àö√∫‚Äö√Ñ√∂‚àö‚Ä†‚àö‚àÇ‚Äö√†√∂‚àö¬∞‚Äö√Ñ√∂‚àö‚Ä†‚àö‚àÇ‚Äö√†√∂‚Äö√†¬¥‚Äö√Ñ√∂‚àö‚Ä†‚àö‚àÇ‚Äö√Ñ√∂‚àö√ë¬¨¬¢‚Äö√Ñ√∂‚àö‚Ä†‚àö‚àÇ‚Äö√†√∂¬¨√ü‚Äö√Ñ√∂‚àö‚Ä†‚àö‚àÇ‚Äö√Ñ√∂‚àö√ë‚Äö√Ñ‚Ä†‚Äö√Ñ√∂‚àö‚Ä†‚àö‚àÇ‚Äö√Ñ√∂‚àö√´¬¨¬¢‚Äö√Ñ√∂‚àö‚Ä†‚àö‚àÇ‚Äö√†√∂‚àö¬∞‚Äö√Ñ√∂‚àö‚Ä†‚àö‚àÇ¬¨¬®‚àö√º‚Äö√Ñ√∂‚àö‚Ä†‚àö‚àÇ¬¨¬®‚Äö√†¬¥')</script>");
//            out.println("<SCRIPT   LANGUAGE='JavaScript'>");  
//out.println("<!--");  
//out.println("alert('successfully register, click ok to edit you profile')");  
// out.println("//-->");  
// out.println("</SCRIPT>");
            request.getSession().setAttribute("user", viewer);
            request.getRequestDispatcher("myprofile.jsp").forward(request, response);
            request.getRequestDispatcher("/footer.html").include(request, response);
            
            
            
            }
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
        
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
